package location

import (
	"context"
	"device-admin/internal/app/grpc/client"
	"device-admin/internal/app/grpc/protoc/system_admin"
	"device-admin/internal/dal"
	"device-admin/internal/dao"
	"device-admin/internal/define"
	"device-admin/internal/models"
	"strings"

	"192.168.1.75/go-pkg/errorx"
)

type Tag struct{}

// AddLocationTag 添加位置标签
func (Tag) AddLocationTag(ctx context.Context, tagName, merchantID string) (string, error) {
	if merchantID == "" {
		return "", errorx.New("位置商户不能为空", -1)
	}
	return dao.LocationTag{}.Add(ctx, dal.Q, tagName, merchantID)
}

// UpdateLocationTag 更新位置标签
func (Tag) UpdateLocationTag(ctx context.Context, tagID, tagName, merchantID string) error {
	return dao.LocationTag{}.Update(ctx, dal.Q, tagID, tagName, merchantID)
}

// DeleteLocationTag 删除位置标签
func (Tag) DeleteLocationTag(ctx context.Context, tagID, merchantID string) error {
	if (dao.ObjAssociation{}).Exists(ctx, dal.Q, "", tagID) {
		return errorx.New("标签正在使用", -1)
	}
	return dao.LocationTag{}.Delete(ctx, dal.Q, tagID, merchantID)
}

// LocationTagList 位置标签列表
func (Tag) LocationTagList(
	ctx context.Context,
	tagName, merchantID string,
	page, limit int,
) (
	list []*models.LocationTag,
	total int64,
	err error,
) {
	return dao.LocationTag{}.List(ctx, dal.Q, tagName, merchantID, page, limit)
}

// AddTagLocationAssociation 增加标签位置关联
// 位置<->标签
func (Tag) AddLocationTagAssociation(ctx context.Context, locationID, tagID string) ([]string, error) {
	locationIDs := strings.Split(locationID, ",")
	objAssociations := []*models.ObjAssociation{}
	for _, locationID := range locationIDs {
		// 关联对象数组
		objAssociations = append(objAssociations, &models.ObjAssociation{
			AssociationType: int(define.AssociationLocationTag),
			ObjID:           locationID,
			AssociationID:   tagID,
		})
	}
	return dao.ObjAssociation{}.Add(
		ctx, dal.Q,
		objAssociations,
		1,
		dao.Location{}.Exists,
		dao.LocationTag{}.Exists,
	)
}

// DeleteTagLocationAssociation 移除标签位置关联
// 位置<->标签
func (Tag) DeleteLocationTagAssociation(ctx context.Context, locationIDs []string, tagID string) error {
	for _, locationID := range locationIDs {
		dao.ObjAssociation{}.Delete(ctx, dal.Q, define.AssociationLocationTag, locationID, tagID)
	}
	return nil
}

// TagLocationList 标签位置列表
func (Tag) TagLocationList(
	ctx context.Context,
	tagID string,
	locationName string,
	page, limit int,
) (
	list []*dao.TagLocationListType,
	total int64,
	err error,
) {
	if tagID == "" {
		return nil, 0, errorx.New("标签id不能为空", -1)
	}
	list, total, err = dao.LocationTag{}.LocationList(ctx, dal.Q, tagID, locationName, page, limit)
	// 组合区域id
	areaIDs := []string{}
	for _, record := range list {
		areaIDs = append(areaIDs, record.ProvinceID, record.CityID, record.DistrictID)
	}
	// 获取省市区的名称
	{
		cli, err := client.GetSystemAdminClient()
		if err == nil {
			rsp, err := cli.AreaName(context.Background(), &system_admin.AreaNameReq{AreaIds: areaIDs})
			if err == nil && rsp != nil {
				for index, record := range list {
					list[index].Province = rsp.AreaNameMap[record.ProvinceID]
					list[index].City = rsp.AreaNameMap[record.CityID]
					list[index].District = rsp.AreaNameMap[record.DistrictID]
				}
			}
		}
	}
	return list, total, err
}
